package reflectdemo1;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectClassDemo {

    // 创建对象
    public static void reflectNewInstance(){
        try {
            Class<?> c1 = Class.forName("reflectdemo1.Student");
            Student student = (Student)c1.newInstance();// 实例化一个 Student对象
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    // 反射私有的构造方法 屏蔽内容为获得公有的构造方法
    public static void reflectPrivateConstructor(){
        try {
            Class<?> c1 = Class.forName("reflectdemo1.Student");
            Constructor<?> constructor = c1.getDeclaredConstructor(String.class, int.class);
            constructor.setAccessible(true);
            Student student = (Student) constructor.newInstance("haha", 55);
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    // 反射私有属性
    public static void reflectPrivateField(){
        try {
            Class<?> c1 = Class.forName("reflectdemo1.Student");
            Student student = (Student) c1.newInstance();

            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);
            field.set(student,"张三");// 修改私有属性
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    // 反射私有方法
    public static void reflectPrivateMethod(){
        try {
            Class<?> c1 = Class.forName("reflectdemo1.Student");
            Student student = (Student) c1.newInstance();
            Method function = c1.getDeclaredMethod("function", String.class);
            function.setAccessible(true);
            function.invoke(student,"我是私有的方法的参数");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
//        reflectNewInstance();
//        reflectPrivateConstructor();
//        reflectPrivateField();
        reflectPrivateMethod();
    }
}
